package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"strconv"
	"strings"
)

func ReListPageSizeHandler() (pageSize int) {
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	data := map[string]string{
		"pageNo":     "1",
		"pageSize":   "30",
		"djzh":       "",
		"nymc":       "",
		"cjmc":       "",
		"sf":         "",
		"nylb":       "",
		"zhl":        "",
		"jx":         "",
		"zwmc":       "",
		"fzdx":       "",
		"syff":       "",
		"dx":         "",
		"yxcf":       "",
		"yxcf_en":    "",
		"yxcfhl":     "",
		"yxcf2":      "",
		"yxcf2_en":   "",
		"yxcf2hl":    "",
		"yxcf3":      "",
		"yxcf3_en":   "",
		"yxcf3hl":    "",
		"yxqs_start": "",
		"yxqs_end":   "",
		"yxjz_start": "",
		"yxjz_end":   "",
		"accOrfuzzy": "2",
	}

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")
		r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		// fmt.Println(htmlquery.OutputHTML(doc, false))
		node := htmlquery.FindOne(doc, `/html/body/div[1]/div/ul/li[11]/a`)

		pageSize, err = strconv.Atoi(htmlquery.InnerText(node))
		if err != nil {
			fmt.Println("strconv.Atoi err")
		}

	})
	c.Post("https://www.icama.cn/BasicdataSystem/pesticideRegistration/queryselect.do", data)

	return
}

func ReListHandler(pageSize int) (code, viewpd []string, err error) {
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
	// c.OnHTML("tbody", func(e *colly.HTMLElement) {
	// 	e.Request.Visit(e.Attr("onclick"))
	// })
	pageNo := strconv.Itoa(pageSize)
	data := map[string]string{
		"pageNo":     pageNo,
		"pageSize":   "30",
		"djzh":       "",
		"nymc":       "",
		"cjmc":       "",
		"sf":         "",
		"nylb":       "",
		"zhl":        "",
		"jx":         "",
		"zwmc":       "",
		"fzdx":       "",
		"syff":       "",
		"dx":         "",
		"yxcf":       "",
		"yxcf_en":    "",
		"yxcfhl":     "",
		"yxcf2":      "",
		"yxcf2_en":   "",
		"yxcf2hl":    "",
		"yxcf3":      "",
		"yxcf3_en":   "",
		"yxcf3hl":    "",
		"yxqs_start": "",
		"yxqs_end":   "",
		"yxjz_start": "",
		"yxjz_end":   "",
		"accOrfuzzy": "2",
	}

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")
		r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `//*[@id="tab"]/tbody/tr`)
		for key, node := range nodes {
			if key == 0 {
				continue
			}
			a := htmlquery.FindOne(node, "./td[1]/span/a")
			str := htmlquery.SelectAttr(a, "onclick")
			code = append(code, htmlquery.InnerText(a))
			viewpd = append(viewpd, str[:len(str)-2][9:])
		}
	})
	c.Post("https://www.icama.cn/BasicdataSystem/pesticideRegistration/queryselect.do", data)
	return
}
